// stl
#include <iostream>
#include <unordered_map>
#include <format>
// raylib
#include <raylib.h>
// maze
#include <win32_utils.h>
#include <scene_manager.h>
#include <GameScene.h>
#include "hero.h"
#include "scene_start.cpp"
#include "scene_gaming.cpp"
#include "scene_randmap.cpp"
#include "scene_raycasting.cpp"
#include "scene_east.cpp"
#include "scene_loading.cpp"


int main(void){
    SetDPIAware();
    SetConfigFlags(FLAG_WINDOW_RESIZABLE);

    InitWindow(2200, 1500, "Maze");
    SetTargetFPS(120);

    SetExitKey(KEY_NULL); // 禁用esc键退出
    DisableIME(); // windows平台禁用中文输入法

    Image logo130 = LoadImage("assets/logo130.png");
    if(IsImageValid(logo130))
        SetWindowIcon(logo130);

    char chars[] = "MAZEqwertyuiopasdfghjkl;'zxcvbnm,./1234567890-=!@#$%^&*()_+}{[]:?><MNBVCXZAQWSEDRFTGYHUJIKOLP:汉字显示占位改变占位.";
    int fontFileSize;
    unsigned char *fontFileData = LoadFileData("c:\\windows\\fonts\\simhei.ttf", &fontFileSize);
    int codepointsCount;
    int *codepoints = LoadCodepoints(chars, &codepointsCount);
    Font font = LoadFontFromMemory(".ttf", fontFileData, fontFileSize, 64, codepoints, codepointsCount);

    std::unordered_map<std::string, Texture2D> texture_blocks;
    texture_blocks.emplace("end",LoadTexture("assets/textures/mc/diamond.png"));
    texture_blocks.emplace("start",LoadTexture("assets/textures/mc/stone.png"));
    texture_blocks.emplace("floor",LoadTexture("assets/textures/mc/dirt.png"));
    texture_blocks.emplace("wall",LoadTexture("assets/textures/mc/bricks.png"));
    texture_blocks.emplace("grass",LoadTexture("assets/textures/mc/grass.png"));
    texture_blocks.emplace("lava",LoadTexture("assets/textures/mc/magma.png"));

    string imgPath = "./assets/imgs/characters/character.png";
    Hero hero(imgPath);

    UnloadCodepoints(codepoints);

    SceneManager sceneManager;
    sceneManager.put("start", new SceneSatrt(sceneManager, &font));
    sceneManager.put("loading", new SceneLoading(sceneManager,texture_blocks, &font));
    sceneManager.put("gaming", new SceneGaming(sceneManager, texture_blocks, &font));
    sceneManager.put("randmap", new SceneRandMap(sceneManager, texture_blocks, &font));
    sceneManager.put("Game", new GameScene(sceneManager,texture_blocks,&font,hero));
    sceneManager.put("raycasting", new SceneRaycasting());
    sceneManager.put("east", new SceneEast());

    while (!WindowShouldClose())
    {
        ClearBackground(RAYWHITE);

        sceneManager.render(GetFrameTime());

        if(IsKeyPressed(KEY_ESCAPE)) sceneManager.switchTo("start");

        // 绘制fps
        BeginDrawing();
        DrawFPS(0, 0);
        // DrawText(std::format("{}",GetFrameTime()).c_str(),0,60,16,GREEN);
        EndDrawing();
    }

    UnloadFont(font);
    UnloadFileData(fontFileData);
    for(auto texture : texture_blocks)
    {
        UnloadTexture(texture.second);
    }

    CloseWindow();
    return 0;
}